package gunfire.actions;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.*;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import gunfire.GunfireMod;
import gunfire.powers.BurningPower;
import gunfire.powers.DecayPower;
import gunfire.powers.ManipulationPower;
import gunfire.powers.ShockPower;
import gunfire.relics.SoulOfElement;

public class ElementalEffectAction extends AbstractGameAction {

    private final AbstractCreature target;
    private final AbstractCreature source;
    private int amount;
    private final GunfireMod.ElementalEffect ee;

    public ElementalEffectAction(AbstractCreature target, AbstractCreature source, int amount, GunfireMod.ElementalEffect ee){
        this.target = target;
        this.source = source;
        this.amount = amount;
        this.ee = ee;
        AbstractPlayer p = AbstractDungeon.player;
        if(p != null && p.hasRelic("gunfire:SoulOfElement2") && ee != GunfireMod.ElementalEffect.Combustion){
            this.amount++;
            p.getRelic("gunfire:SoulOfElement2").flash();
        }
    }

    //造成元素异常时
    private void triggerElementEffect(){
        AbstractPlayer p = AbstractDungeon.player;
        if(p != null && p.hasRelic("gunfire:SoulOfElement") && this.target != null && !this.target.hasPower("Artifact")){
            SoulOfElement r = (SoulOfElement) p.getRelic("gunfire:SoulOfElement");
            r.flash();
            r.progress2 += this.amount;
            r.judgeEvolve();
        }
    }

    //造成元素融合时
    //遗物：元素馈赠
    private void triggerElementMix(){
        AbstractPlayer p = AbstractDungeon.player;
        if(p != null && p.hasRelic("gunfire:ElementalBestowment")){
            AbstractRelic r = p.getRelic("gunfire:ElementalBestowment");
            r.flash();
            this.addToBot(new GainEnergyAction(1));
            this.addToBot(new HealAction(p, null, 2));
        }
    }

    @Override
    public void update() {
        if(this.target != null){
            switch (this.ee){
                case Burning:
                    this.triggerElementEffect();
                    if(target.hasPower("gunfire:DecayPower")){
                        //毒+火
                        this.addToTop(new RemoveSpecificPowerAction(target, source, "gunfire:DecayPower"));
                        int damage;
                        AbstractPlayer p = AbstractDungeon.player;
                        if(p != null && p.hasRelic("gunfire:SoulOfElement2")){
                            damage = amount * 6 * 2;
                            p.getRelic("gunfire:SoulOfElement2").flash();
                        } else {
                            damage = amount * 4 * 2;
                        }
                        this.addToBot(new ElementalEffectAction(target, source, damage, GunfireMod.ElementalEffect.Combustion));
                        if(this.source.hasPower("gunfire:ElementalChainPower")){
                            this.addToTop(new ApplyPowerAction(target, source, new BurningPower(target, source, amount), amount));
                        }
                    } else if(target.hasPower("gunfire:ShockPower")){
                        //电+火
                        this.addToTop(new RemoveSpecificPowerAction(target, source, "gunfire:ShockPower"));
                        this.addToBot(new ElementalEffectAction(target, source, amount, GunfireMod.ElementalEffect.Manipulation));
                        if(this.source.hasPower("gunfire:ElementalChainPower")){
                            this.addToTop(new ApplyPowerAction(target, source, new BurningPower(target, source, amount), amount));
                        }
                    } else {
                        //火
                        this.addToTop(new ApplyPowerAction(target, source, new BurningPower(target, source, amount), amount));
                    }
                    break;
                case Decay:
                    this.triggerElementEffect();
                    if(target.hasPower("gunfire:BurningPower")){
                        //火+毒
                        this.addToTop(new RemoveSpecificPowerAction(target, source, "gunfire:BurningPower"));
                        int damage;
                        AbstractPlayer p = AbstractDungeon.player;
                        if(p != null && p.hasRelic("gunfire:SoulOfElement2")){
                            damage = target.getPower("gunfire:BurningPower").amount * 6 * 2;
                            p.getRelic("gunfire:SoulOfElement2").flash();
                        } else {
                            damage = target.getPower("gunfire:BurningPower").amount * 4 * 2;
                        }
                        this.addToBot(new ElementalEffectAction(target, source, damage, GunfireMod.ElementalEffect.Combustion));
                        if(this.source.hasPower("gunfire:ElementalChainPower")){
                            this.addToTop(new ApplyPowerAction(target, source, new DecayPower(target, source, amount), amount));
                        }
                    } else if(target.hasPower("gunfire:ShockPower")){
                        //电+毒
                        this.addToTop(new RemoveSpecificPowerAction(target, source, "gunfire:ShockPower"));
                        this.addToBot(new ElementalEffectAction(target, source, amount, GunfireMod.ElementalEffect.Miasma));
                        if(this.source.hasPower("gunfire:ElementalChainPower")){
                            this.addToTop(new ApplyPowerAction(target, source, new DecayPower(target, source, amount), amount));
                        }
                    } else {
                        //毒
                        this.addToTop(new ApplyPowerAction(target, source, new DecayPower(target, source, amount), amount));
                    }
                    break;
                case Shock:
                    this.triggerElementEffect();
                    if(target.hasPower("gunfire:BurningPower")){
                        //火+电
                        this.addToTop(new RemoveSpecificPowerAction(target, source, "gunfire:BurningPower"));
                        this.addToBot(new ElementalEffectAction(target, source, amount, GunfireMod.ElementalEffect.Manipulation));
                        if(this.source.hasPower("gunfire:ElementalChainPower")){
                            this.addToTop(new ApplyPowerAction(target, source, new ShockPower(target, source, amount), amount));
                        }
                    } else if(target.hasPower("gunfire:DecayPower")){
                        //毒+电
                        this.addToTop(new RemoveSpecificPowerAction(target, source, "gunfire:DecayPower"));
                        this.addToBot(new ElementalEffectAction(target, source, amount, GunfireMod.ElementalEffect.Miasma));
                        if(this.source.hasPower("gunfire:ElementalChainPower")){
                            this.addToTop(new ApplyPowerAction(target, source, new ShockPower(target, source, amount), amount));
                        }
                    } else {
                        //电
                        this.addToTop(new ApplyPowerAction(target, source, new ShockPower(target, source, amount), amount));
                    }
                    break;
                case Combustion:
                    this.addToBot(new DamageAction(target, new DamageInfo(source, amount, DamageInfo.DamageType.NORMAL), AttackEffect.FIRE));
                    this.triggerElementMix();
                    break;
                case Manipulation:
                    this.addToBot(new ApplyPowerAction(source, source, new ManipulationPower(source, 1), 1));
                    this.triggerElementMix();
                    break;
                case Miasma:
                    AbstractPlayer p = AbstractDungeon.player;
                    int damage;
                    if(p != null && p.hasRelic("gunfire:SoulOfElement2")){
                        damage = (int) (0.25F * target.maxHealth);
                        p.getRelic("gunfire:SoulOfElement2").flash();
                    } else {
                        damage = (int) (0.15F * target.maxHealth);
                    }
                    this.addToBot(new DamageAllEnemiesAction(null, DamageInfo.createDamageMatrix(damage, true), DamageInfo.DamageType.HP_LOSS, AttackEffect.POISON));
                    this.triggerElementMix();
                    break;
            }
        }

        this.isDone = true;
    }
}
